<?php

namespace app\admin\controller\knowbase;

use app\admin\controller\auth\Group;
use app\admin\model\AuthGroup;
use app\common\controller\Backend;
use app\common\exception\UploadException;
use app\common\library\Upload;
use fast\Tree;
use think\Config;
use think\Db;
use think\Exception;
use think\File;

/**
 * 自定义上传
 *
 */
class Ajax extends \app\admin\controller\Ajax
{
    /**
     * 重写上传文件逻辑
     */
    public function upload()
    {
        Config::set('default_return_type', 'json');

       
       

        $chunkid = $this->request->post("chunkid");
        if ($chunkid) {
            if (!Config::get('upload.chunking')) {
                $this->error(__('Chunk file disabled'));
            }
            $action = $this->request->post("action");
            $chunkindex = $this->request->post("chunkindex/d");
            $chunkcount = $this->request->post("chunkcount/d");
            $filename = $this->request->post("filename");
            $method = $this->request->method(true);
            if ($action == 'merge') {
                $attachment = null;
               
                try {
                    $upload = new Upload();
                    $attachment = $upload->merge($chunkid, $chunkcount, $filename);
                } catch (UploadException $e) {
                    $this->error($e->getMessage());
                }
                $this->success(__('Uploaded successful'), '', ['url' => $attachment->url, 'fullurl' => cdnurl($attachment->url, true)]);
            } elseif ($method == 'clean') {
               
                try {
                    $upload = new Upload();
                    $upload->clean($chunkid);
                } catch (UploadException $e) {
                    $this->error($e->getMessage());
                }
                $this->success();
            } else {
               
               
                $file = $this->request->file('file');
                try {
                    $upload = new Upload($file);
                    $upload->chunk($chunkid, $chunkindex, $chunkcount);
                } catch (UploadException $e) {
                    $this->error($e->getMessage());
                }
                $this->success();
            }
        } else {
            $attachment = null;
            $param = request()->param();
            $url = '';
            if (isset($param['url']) && is_string($param['url']) && strpos($param['url'], 'http')===0) {
                $content = file_get_contents($param['url']);
                $url = $param['url'];
               
                $tmpFile = tempnam(sys_get_temp_dir(), 'temp-knowbase');
                file_put_contents($tmpFile, $content);
                $file = new File($tmpFile);
                $file->isTest(true);
                $name = uniqid().'.'.explode("/", $file->getMime())[1];
                $info = [
                    'name'     => $name,
                    'type'     => $file->getMime(),
                    'tmp_name' => $tmpFile,
                    'error'    => 0,
                    'size'     => $file->getSize()
                ];
                $file->setUploadInfo($info);
            } else {
               
                $file = $this->request->file('file');
            }
            $data = [];
            try {
                if (is_array($file)) {
                    foreach ($file as $k=>$f) {
                        $upload = new Upload($f);
                        $attachment = $upload->upload();
                        $data[] =  ['url' => $attachment->url, 'fullurl' => cdnurl($attachment->url, true),'filename'=>$attachment->filename];
                    }
                } else {
                    $upload = new Upload($file);
                    $attachment = $upload->upload();
                    $data =  ['original'=>$url,'url' => $attachment->url, 'fullurl' => cdnurl($attachment->url, true),'filename'=>$attachment->filename];
                }
            } catch (UploadException $e) {
                $this->error($e->getMessage());
            }

            $this->success(__('Uploaded successful'), '', $data);
        }
    }

}
